package com.wang.auth.aop;

import com.wang.auth.service.SysDeptService;
import com.wang.auth.service.SysOperLogService;
import com.wang.auth.service.SysUserService;
import com.wang.common.utils.IpUtils;
import com.wang.model.system.SysDept;
import com.wang.model.system.SysOperLog;
import com.wang.model.system.SysUser;
import com.wang.security.custom.LoginInfoHelper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author wjh
 * @create 2023-08-16 22:18
 */
@Aspect
@Component
public class LogAspect {

    @Autowired
    SysOperLogService operLogService;

    @Autowired
    SysUserService userService;

    @Autowired
    SysDeptService deptService;

    @Pointcut("execution(* com.wang.auth.controller.SysUserController.save(..)) ||" +
            "execution(* com.wang.auth.controller.SysUserController.updateById(..) )||" +
            "execution(* com.wang.auth.controller.SysUserController.updateStatus(..))||" +
            "execution(* com.wang.auth.controller.SysUserController.remove(..))")
    public void userOperations() {}

    @Pointcut("execution(* com.wang.auth.controller.SysRoleController.save(..)) ||" +
            "execution(* com.wang.auth.controller.SysRoleController.batchRemove(..)) ||" +
            "execution(* com.wang.auth.controller.SysRoleController.updateById(..)) ||" +
            "execution(* com.wang.auth.controller.SysRoleController.deleteById(..)) ||" +
            "execution(* com.wang.auth.controller.SysRoleController.doAssign(..))")
    public void roleOperations() {}

    @Pointcut("execution(* com.wang.auth.controller.SysMenuController.save(..)) ||" +
            "execution(* com.wang.auth.controller.SysMenuController.doAssign(..)) ||" +
            "execution(* com.wang.auth.controller.SysMenuController.updateById(..)) ||" +
            "execution(* com.wang.auth.controller.SysMenuController.remove(..))")
    public void menuOperations() {}


    @Pointcut("execution(* com.wang.auth.controller.SysDeptController.save(..)) ||" +
            "execution(* com.wang.auth.controller.SysDeptController.deleteById(..)) ||" +
            "execution(* com.wang.auth.controller.SysDeptController.updateById(..)) ||" +
            "execution(* com.wang.auth.controller.SysDeptController.batchRemove(..))")
    public void deptOperations() {}

    @Pointcut("execution(* com.wang.auth.controller.SysPostController.save(..)) ||" +
            "execution(* com.wang.auth.controller.SysPostController.deleteById(..)) ||" +
            "execution(* com.wang.auth.controller.SysPostController.updateById(..)) ||" +
            "execution(* com.wang.auth.controller.SysPostController.batchRemove(..))")
    public void postOperations() {}

    @Before("userOperations()")
    public void logUserOperation(JoinPoint joinPoint) {
        createOperLog(joinPoint,"用户管理");
    }

    @Before("roleOperations()")
    public void logRoleOperation(JoinPoint joinPoint) {
        createOperLog(joinPoint, "角色管理");
    }

    @Before("menuOperations()")
    public void logMenuOperation(JoinPoint joinPoint) {
        createOperLog(joinPoint,"菜单管理");
    }

    @Before("deptOperations()")
    public void logOperOperation(JoinPoint joinPoint) {
        createOperLog(joinPoint,"部门管理");
    }

    @Before("postOperations()")
    public void logPostOperation(JoinPoint joinPoint) {
        createOperLog(joinPoint,"岗位管理");
    }

    private void createOperLog(JoinPoint joinPoint,String title) {
        String methodName = joinPoint.getSignature().getName();
        String operation = getOperationFromMethodName(methodName);
        SysOperLog sysOperLog = new SysOperLog();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Long userId = LoginInfoHelper.getUserId().get();
        String userName = LoginInfoHelper.getUsername().get();
        SysUser user = userService.getUserByUserName(userName);
        SysDept dept = deptService.getById(user.getDeptId());
        String ip = IpUtils.getRealIp(request);
        sysOperLog.setOperIp(ip);
        sysOperLog.setTitle(title);
        sysOperLog.setBusinessType(operation);
        sysOperLog.setMethod(methodName);
        sysOperLog.setRequestMethod(request.getMethod());
        sysOperLog.setOperatorType("后台用户");
        sysOperLog.setOperName(user.getName());
        sysOperLog.setCreateTime(LocalDateTime.now());
        sysOperLog.setOperTime(LocalDateTime.now());
        sysOperLog.setDeptName(dept.getName());
        operLogService.save(sysOperLog);
    }




    private String getOperationFromMethodName(String methodName) {
        if (methodName.startsWith("add") || methodName.startsWith("save")) {
            return "新增";
        } else if (methodName.startsWith("update")) {
            return "修改";
        } else if (methodName.startsWith("delete") || methodName.startsWith("remove")) {
            return "删除";
        } else if (methodName.startsWith("batchRemove")){
            return "批量删除";
        } else if (methodName.startsWith("doAssign")) {
            return "分配权限";
        } else return "其他操作";
    }
}
